//{=================================================================================
//! @file    sudoku.c
//! @date    2013-10-09 01:21
//! @author  Ivan Golovanov <ivan0golovanov@gmail.com>
//! @version 0.1
//!
//! This program checks the Sudoku solution and returns "YES" if solved correctly
//!                                                 and "NO" if solved wrong.
//! @par     Task:
//!          Input is a 9x9 table of digits(1..9)(Sudoku).
//!          Define whether it is solved correctly and return "YES" or "NO".
//! @warning Input is digits (1..9) only.
//}=================================================================================

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#define VERIFY(cond) if (!cond) printf("All is bad %s",__LINE__);
#define SET_TO_ZERO(arr) arr[1]=arr[2]=arr[3]=arr[4]=arr[5]=arr[6]=arr[7]=arr[8]=arr[9]=0;

//{=================================================================================
//! checkSquare - checks a square (3x3) of digits to find out if there are repeated digits in it.
//!
//! @param      c   Line number
//! @param      b   Column number
//! @param      a   Entered sudoku
//! @return         1 if no repeated digits in the square
//!                 0 if there are repeated digits in the square
//}=================================================================================

int checkSquare(int c,int b, int a[9][9])
{
int i,j,k,digit[10]={0,0,0,0,0,0,0,0,0,0},check=1;
for (i = c;i < c + 3;i++)
    for (j = b;j < b + 3;j++)
        {
            k = a[i][j];
            if (digit[k]==1)
                check = 0;
            else digit[k] = 1;
         }
if (check==0) return(0); else return(1);
}

//{=================================================================================
//! @param      ans   Answer to the question.
//! @param      i   Runner
//! @param      j   Runner
//! @param      dec Auxiliary array, to tell if the digit was already found
//! @param      a  Sudoku array
//!
//! @return         Number of roots or zero if none, -1 if infinite number of roots
//!
//! @note           Calculation precision is considered to be DBL_EPSILON.
//}=================================================================================

int main()
{   int ans,i,j,dec[10]={0,0,0,0,0,0,0,0,0,0};
    int a[9][9]={6, 4, 2, 1, 8, 3, 5, 9, 7,
                 3, 1, 9, 5, 2, 7, 6, 8, 4,
                 5, 8, 7, 9, 4, 6, 3, 2, 1,
                 8, 9, 4, 2, 6, 5, 7, 1, 3,
                 7, 3, 6, 8, 1, 9, 2, 4, 5,
                 2, 5, 1, 7, 3, 4, 9, 6, 8,
                 4, 6, 5, 3, 9, 1, 8, 7, 2,
                 1, 7, 8, 6, 5, 2, 4, 3, 9,
                 9, 2, 3, 4, 7, 8, 1, 5, 6};
printf("Hello! This program will check the correctness of Sudoku.\n");
printf("Enter 1 if you'd like to enter new Sudoku or 2 if you are OK with build-in one.\n");
VERIFY((scanf("%d",&ans) == 1))
if (ans == 1) {
///////////////////////////////////////////////////////////////////////////////////////////////
    for (i = 0; i < 9; i++)
        for ( j = 0; j < 9; j++)
            {
            VERIFY(!(i > 8) && !(j > 8));
            VERIFY( (scanf("%d",&a[i][j])) == 1)
            }
///////////////////////////////////////////////////////////////////////////////////////////////
               }
    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9; j++)
            {
            VERIFY(!(i > 8) && !(j > 8));
            if (dec[a[i][j]]==1)
                {
                printf("NO"); goto exit;;
                }
            else dec[a[i][j]]=1;
            }
         SET_TO_ZERO(dec)
    }
SET_TO_ZERO(dec)
///////////////////////////////////////////////////////////////////////////////////////////////
    for (i = 0; i < 9; i++)
    {
        for ( j = 0; j < 9; j++)
            {
            VERIFY(!(i > 8) && !(j > 8));
            if (dec[a[j][i]]==1)
                {
                printf("NO"); goto exit;;
                }
            else dec[a[j][i]]=1;
            }
        SET_TO_ZERO(dec)
    }
///////////////////////////////////////////////////////////////////////////////////////////////
    for (i = 0; i<=6; i = i + 3)
        for (j = 0; j<=6; j = j + 3)
            {
             VERIFY(!(i > 8) && !(j > 8));
             if (checkSquare(i,j,a)==0)
                {
                printf("NO"); goto exit;
                }
            }
///////////////////////////////////////////////////////////////////////////////////////////////
 printf("YES");
 exit: return(0);
}
